﻿using System.DrawingCore;
using System.DrawingCore.Imaging;
using System.IO;
using ZXing;
using ZXing.Common;
using ZXing.QrCode.Internal;
using ZXing.ZKWeb.Rendering;

namespace JNPF.Utils
{
    /// <summary>
    /// 二维码生成帮助类
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// </summary>
    public class QRCodeHelper
    {
        #region 生成二维码
        /// <summary>
        /// 生成二维码,自定义边长
        /// </summary>
        /// <param name="content">二维码内容</param>
        /// <param name="imgSize">二维码边长px</param>
        /// <returns></returns>
        public static MemoryStream BuildQRCode(string content, int imgSize)
        {
            var img = BuildQRCode(content, imgSize, Color.White, Color.Black);
            var ms = new MemoryStream();
            img.Save(ms, ImageFormat.Png);
            return ms;
        }
        /// <summary>
        /// 生成二维码
        /// 注：自定义边长以及颜色
        /// </summary>
        /// <param name="content">二维码内容</param>
        /// <param name="imgSize">二维码边长px</param>
        /// <param name="background">二维码底色</param>
        /// <param name="foreground">二维码前景色</param>
        /// <returns></returns>
        public static Image BuildQRCode(string content, int imgSize, Color background, Color foreground)
        {
            var writer = new ZXing.ZKWeb.BarcodeWriter
            {
                Renderer = new BitmapRenderer { Background = background, Foreground = foreground },
                Format = BarcodeFormat.QR_CODE
            };
            writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
            writer.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            writer.Options.Height = writer.Options.Width = imgSize;
            writer.Options.Margin = 0;
            var img = writer.Write(content);
            return img;
        }
        #endregion

        #region 生成条形码
        /// <summary>
        /// 生成条形码
        /// 注：自定义尺寸
        /// </summary>
        /// <param name="content">条形码内容</param>
        /// <param name="width">宽度px</param>
        /// <param name="height">高度px</param>
        /// <returns></returns>
        public static MemoryStream BuildBarCode(string content, int width, int height)
        {
            var writer = new ZXing.ZKWeb.BarcodeWriter
            {
                Format = BarcodeFormat.CODE_128
            };
            var options = new EncodingOptions()
            {
                Width = width,
                Height = height,
                Margin = 2
            };
            writer.Options = options;
            var map = writer.Write(content);
            var ms = new MemoryStream();
            map.Save(ms, ImageFormat.Png);
            return ms;
        }
        #endregion

        #region 读取码内容
        /// <summary>
        /// 从二维码读取内容
        /// </summary>
        /// <param name="image">二维码</param>
        /// <returns></returns>
        public static string ReadContent(Bitmap image)
        {
            var reader = new ZXing.ZKWeb.BarcodeReader();
            reader.Options.CharacterSet = "UTF-8";
            var result = reader.Decode(image);
            return result == null ? "" : result.Text;
        }
        #endregion

        #region 将Bitmap 转换byte[]的方法
        public static byte[] BitmapToArray(Bitmap bmp)
        {
            byte[] byteArray = null;
            using (var stream = new MemoryStream())
            {
                bmp.Save(stream, ImageFormat.Png);
                byteArray = stream.GetBuffer();
            }
            return byteArray;
        }
        #endregion
    }
}
